/*
 * Copyright 2023 devaper
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.github.devaper.pigeon.factory

import io.github.devaper.pigeon.component.*
import io.github.devaper.pigeon.intercept.*
import java.util.function.Supplier

/**
 * 在创建Client代理时需要提供的配置对象
 *
 * @param basePathSupplier 请求的基础路径提供函数, 优先级高于basePath
 * @param httpClientEngine [HttpClientEngine] Http客户端引擎, 必须提供
 * @param httpMessageConverter [HttpMessageConverter] Http消息转换器, 必须提供
 * @param requestInterceptor [RequestInterceptor] 可选, 多个请传入Composite
 * @param fallbackInstance fallback实例, 该实例需要去实现Client接口, 如果调用失败, 那么将会调用fallback中的对应方法
 * @param retry [RetryConfig]
 */
data class ClientConfig(
    var basePathSupplier: Supplier<String>? = null,
    var httpClientEngine: HttpClientEngine? = null,
    var httpMessageConverter: HttpMessageConverter? = null,
    var requestInterceptor: RequestInterceptor? = null,
    var fallbackInstance: Any? = null,
    var retry: RetryConfig? = null
) {

    private var fromClone = false

    fun clone(): ClientConfig {
        return if (fromClone) {
            this
        } else {
            val ncc = copy(
                retry = retry?.clone()
            )
            ncc.fromClone = true
            ncc
        }
    }

}